home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 25
/
Cream of the Crop 25.iso
/
bbs
/
mhis020.zip
/
WILDCARD.MH
< prev
Wrap
Text File
|
1996-09-20
|
3KB
|
144 lines
// This file defines functions for performing regular expression searches on
// strings.
//
// Functions:
//
// bool matchWildcard (string: regexp, string: toCompare)
//
//
//
// Description of matchWildcard ()
//
// Compares two strings. The first string may contain wildcards.
// Recognised wildcards are:
//
// * match any 0 or more characters
// ! match anything but the following character
// [...] match any of the characters within the brackets
// ? match any one character
//
// Returns True if the strings match, False otherwise.
//
#ifndef __WILDCARD_MH
#define __WILDCARD_MH
#ifndef __STRING_MH
#include "string.mh"
#endif
#ifndef __GENERAL_MH
#include "general.mh"
#endif
int matchMultiple (string: theString, string: wildcards, int: i1, Ref int: i2);
// This routine is called by matchWildcard. It returns the number of characters
// matched.
int matchSubstr (string: theString, string: wildcards, int: i1, Ref int: i2) {
char: curChar;
curChar := wildcards [i2];
if ((curChar = '?')
or (curChar = theString [i1])) {
i2 := i2 + 1;
return 1;
}
else if (curChar = '\\') {
i2 := i2 + 2;
if (wildcards [i2-1] = theString [i1]) {
return 1;
}
else return 0;
}
else if (curChar = '!') {
i2 := i2 + 1;
if (matchSubstr (theString, wildcards,i1,i2) = 0) {
return 1;
}
else return 0;
}
else if (curChar = '*') {
int: temp1, temp2, r;
if (i2 = strlen (wildcards)) {
i2 := i2 + 1;
return strlen (theString) - i1 + 1;
};
for (temp1 := i1; temp1 <= strlen (theString); temp1 := temp1 + 1) {
temp2 := i2 + 1;
r := matchMultiple (theString, wildcards, temp1, temp2);
if (r = strlen (theString) - temp1 + 1) {
i2 := temp2;
return r + temp1 - i1;
};
};
return 0;
}
else if (curChar = '[') {
int: found, tmp, temp1;
found := 0;
i2 := i2 + 1;
for (; wildcards [i2] <> ']'; ) {
tmp := (wildcards [i2] = theString [i1]);
i2 := i2 + 1;
// tmp := matchSubstr (theString, wildcards, i1, i2);
if (tmp > found) {
found := tmp;
};
};
i2 := i2 + 1;
return found;
}
else {
i2 := i2 + 1;
return 0;
};
}
int matchMultiple (string: theString, string: wildcards, int: i1, Ref int: i2) {
int: temp1, result, len;
len := strlen (theString);
for (temp1 := i1; temp1 <= len; ) {
result := matchSubstr (theString, wildcards, temp1, i2);
if (result) {
temp1 := temp1 + result;
}
else return temp1 - i1;
};
return temp1 - i1;
}
// MatchWildcard compares a string with another string containing wildcards, and
// determines if the two match.
bool matchWildcard (string: theString, string: wildcards) {
int: i1, i2, result;
if (strlen (wildcards) < 2 ) {
if (strlen (wildcards) = 0) return False;
if ((strlen (wildcards) = 1)
and (wildcards [1] = '*')) return True;
};
i2 := 1;
if (matchMultiple (theString, wildcards, 1, i2) = strlen (theString))
if ((i2 >= strlen (wildcards))
) {
if (i2 = strlen (wildcards)) {
return (wildcards [i2] = '*');
};
return True;
}
return False;
}
#endif